package testcase;
 

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.TestCase;
import org.junit.Test;


import static org.junit.Assert.*;

import java.io.*;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import org.junit.Test;

import maincode.*;
import junit.framework.TestCase;
 
//private static final double DELTA = 1e-15;
public class IniReaderTest extends TestCase{
	@Test 
	//Test whether the latest date file can be successfully checked
	//Expected result: Update Needed, data is updated
	public void test1() throws IOException{
		FileWriter outfile=new FileWriter("C:\\Users\\Monica\\Desktop\\CS3343_TestCase_XIE Qiuwen\\dataForTest9\\latest.txt");
		PrintWriter writeHelper=new PrintWriter(outfile);
		writeHelper.print("16398\n");
		writeHelper.close();
		 
		iniReader.readFiles();
		String datainFile="";
		//String data="7.77930314708&7.79686424718&7.79688938375&7.73530119516&";
		//String data="7.7685648026&7.66933319226&7.66650815276&7.68631830874&";
		String data="7.7762315211&7.7685648026&7.7762315211&7.7685648026&";
		File infile=new File("C:\\Users\\Monica\\Desktop\\CS3343_TestCase_XIE Qiuwen\\dataForTest9\\data"+0+".txt");
		Scanner in=new Scanner(infile);			
		
		for(int i=0;i<4;i++){
			datainFile+=String.valueOf(in.nextDouble());
			datainFile+="&";
		}
		
		//System.out.println(data);
		assertEquals(data,datainFile);	
	}
			
	@Test 
	//Test whether the latest date file can be successfully checked
	//Expected result: Test1.2: Update Not Needed
	public void test2() throws IOException{
		Long d=new Date().getTime()/(24*1000*3600);
		
		FileWriter outfile=new FileWriter("C:\\Users\\Monica\\Desktop\\CS3343_TestCase_XIE Qiuwen\\dataForTest9\\latest.txt");
		PrintWriter writeHelper=new PrintWriter(outfile);
		
		writeHelper.print(d+"\n");
		writeHelper.close();
		
		iniReader.readFiles();
		String datainFile="";
		//String data="7.77930314708&7.79686424718&7.79688938375&7.73530119516&";
		//String data="7.7685648026&7.66933319226&7.66650815276&7.68631830874&";
		String data="7.7762315211&7.7685648026&7.7762315211&7.7685648026&";
		File infile=new File("C:\\Users\\Monica\\Desktop\\CS3343_TestCase_XIE Qiuwen\\dataForTest9\\data"+0+".txt");
		Scanner in=new Scanner(infile);			
		
		for(int i=0;i<4;i++){
			datainFile+=String.valueOf(in.nextDouble());
			datainFile+="&";
		}
		
		//System.out.println(data);
		assertEquals(data,datainFile);	
	}

	  
  //Test whether can read correct data into currencyStream
	@Test
	//lists[0][0] usd data0 cur=7.79686424718 pre=7.77930314708 16391 18 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test3() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(0);
		Double result=testCS.getValues()[0];
		//System.out.println("&&"+result);
		assertEquals(-0.00098688994,result,0.00000000001);	
	}

	@Test
	//lists[1][0] cny data1 26 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test4() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(1);
		Double result=testCS.getValues()[0];
		//System.out.println(result);
		assertEquals(-0.000488037470,result,0.0000000001);	
	}

	@Test
	//lists[2][0] jpy data2 26 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test5() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(2);
		Double result=testCS.getValues()[0];
		//System.out.println(result);
		assertEquals(0.00361973371,result,0.0000000001);	
	}

	@Test
	//lists[3][0] eur data3 26 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test6() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(3);
		Double result=testCS.getValues()[0];
		//System.out.println(result);
		assertEquals(-0.00098688994,result,0.0000000001);	
	}
	@Test
	//lists[4][0] aud data4 26 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test7() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(4);
		Double result=testCS.getValues()[0];
		//System.out.println(result);
		assertEquals(0.01111045899,result,0.0000000001);	
	}
	@Test
	//lists[5][0] gbp data5 26 Nov 2014
	//Test whether readFiles can read data in datai.txt and calculate correct result
	public void test8() throws IOException{		
		iniReader.readFiles();
		currencyStream testCS =currencyStream.getCurSet(5);
		Double result=testCS.getValues()[0];
		//System.out.println(result);
		assertEquals(-0.00098688994,result,0.0000000001);	
	}

	@Test
	//Test when no file can be found (checkFileInt)
	public void test9() throws IOException{	
		boolean fileIsFound=iniReader.checkFileInt();
		assertEquals(fileIsFound,false);	
	}
		
}
